<?php


namespace app\common\model;
use think\facade\Db;

class GoodsCate extends Base
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

//求总数或者分页数据
    public function pageData ($param = array(), $range = 'total')
    {
        $where = array();
        //判断是否开启
        if (isset($param['status']) && $param['status'] > -1) {
            $where[] = array('status', '=', $param['status']);
        }
        if (isset($param['parentid']) && $param['parentid'] > -1) {
            $where[] = array('parentid', '=', $param['parentid']);
        }

        //根据分类名称查询
        if (isset($param['goodscatename']) && $param['goodscatename'] != "") {
            $where[] = array('goodscatename', 'like', '%' . $param['goodscatename'] . '%');
        }
        //求总数
        if ($range == 'total') {
            return Db::name('goods_cate')->where($where)->count();
        } else {
            //分页
            $limit = isset($param['limit']) ? (int)$param['limit'] : 0;
            if ($limit > 0) {
                $page = isset($param['page']) ? (int)$param['page'] : 1;
                $limit_start = ($page - 1) * $limit;
                $limit = $limit_start . "," . $limit;
            } else {
                $limit = 0;
            }
            //字段
            $field = isset($param['field']) ? $param['field'] : "";
            //排序
            $order = isset($param['order']) ? $param['order'] : 'id asc';
            return Db::name('goods_cate')->where($where)->field($field)->limit($limit)->order($order)->select()->toArray();
        }

    }



    public function ajaxCateadmin($image,$id = 0){
        if(empty($image)){
            return 1;
        }
        //组装where条件
        $where = array();
        $where[] = array('image','=',$image);
        if($id > 0){
            $where[] =  array('id','<>',$id);
        }
        return Db::name('goods_cate')->where($where)->find();
    }
}